/*
  Copyright © 2014 Allen County Public Library.

  ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.

 * Author: James Harpe
 * 
 * 
 * Description: This adapter is responsible for the layout and content of the search results in SearchResultsActivity.  
 */

package us.in.lib.acpl.Mobile_ACPL.adapters;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import us.in.lib.acpl.Mobile_ACPL.R;
import us.in.lib.acpl.Mobile_ACPL.item.SearchResultTitle;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SearchResultListAdapter extends ArrayAdapter<SearchResultTitle> {
	Context context;
	List<SearchResultTitle> values;
	View rowView;
	List<Bitmap> images;
	int imageListSize = 0;
	int count = 0;

    //ACPL: Removed Syndetics URLs
	public static final String URL_START = "";
	public static final String IMAGE_URL_END = "";


	public SearchResultListAdapter(Context context,
			List<SearchResultTitle> values) {
		super(context, R.layout.search_result_item, values);
		this.context = context;
		this.values = values;
		images = new ArrayList<Bitmap>();

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		rowView = inflater.inflate(R.layout.search_result_item, parent, false);

		if (position >= images.size()) {

			new GetImageTask().execute(rowView, position);

			System.out.println(count++);
		} else {
			View v = rowView.findViewById(R.id.coverBar);
			v.setVisibility(View.GONE);
			ImageView iv = (ImageView) rowView.findViewById(R.id.smallCover);

			Bitmap image = images.get(position);
			if (image == null) {
				String type = values.get(position).getMaterialType();
				if (type.equalsIgnoreCase("Book") || type.equalsIgnoreCase("CR")
						|| type.equalsIgnoreCase("Music")) {

					iv.setImageResource(R.drawable.book);

				} else if (type.equalsIgnoreCase("Sounddisc")
						|| type.equalsIgnoreCase("SOUNDCASS")) {
					
					iv.setImageResource(R.drawable.music);
					
				} else if (type.equalsIgnoreCase("VIDEODISC")
						|| type.equalsIgnoreCase("VIDEOCASS")
						|| type.equalsIgnoreCase("VISUALS")) {
					
					iv.setImageResource(R.drawable.video);

				} else {
					iv.setImageResource(R.drawable.misc);
				}


			} else {
				iv.setImageBitmap(image);

			}
		}
		TextView textView = (TextView) rowView
				.findViewById(R.id.searchResultLine1);
		textView.setText(values.get(position).getTitle());

		textView = (TextView) rowView.findViewById(R.id.searchResultLine2);
		textView.setText(values.get(position).getAuthor());

		textView = (TextView) rowView.findViewById(R.id.searchResultLine3);
		textView.setText("Copies available:\t\t"
				+ values.get(position).getTotalCopiesAvailable());

		return rowView;

	}

	public void setImage(Bitmap image, View rowView, Integer pos) {

		View v = rowView.findViewById(R.id.coverBar);
		v.setVisibility(View.GONE);
		ImageView iv = (ImageView) rowView.findViewById(R.id.smallCover);

		if (image == null || image.getHeight() <= 1) {
			String type = values.get(pos).getMaterialType();
			if (type.equalsIgnoreCase("Book") || type.equalsIgnoreCase("CR")
					|| type.equalsIgnoreCase("Music")) {

				iv.setImageResource(R.drawable.book);

			} else if (type.equalsIgnoreCase("Sounddisc")
					|| type.equalsIgnoreCase("SOUNDCASS")) {
				
				iv.setImageResource(R.drawable.music);
				
			} else if (type.equalsIgnoreCase("VIDEODISC")
					|| type.equalsIgnoreCase("VIDEOCASS")
					|| type.equalsIgnoreCase("VISUALS")) {
				
				iv.setImageResource(R.drawable.video);

			} else {
				iv.setImageResource(R.drawable.misc);
			}

			if (pos >= images.size()) {
				images.add(null);
			} else {
				images.set(pos, null);
			}

		} else {
			iv.setImageBitmap(image);
			if (pos >= images.size()) {
				images.add(image);
			} else {
				images.set(pos, image);
			}
		}

	}

	private class GetImageTask extends AsyncTask<Object, Void, Object[]> {

		@Override
		protected Object[] doInBackground(Object... args) {

			View view = (View) args[0];
			Integer pos = (Integer) args[1];

			String urlString;

			urlString = URL_START + values.get(pos).getIsbn()[0]
					+ IMAGE_URL_END;

			Bitmap coverImage = null;
			try {
				URL newurl = new URL(urlString);
				coverImage = BitmapFactory.decodeStream(newurl.openConnection()
						.getInputStream());

			} catch (Exception e) {
				e.printStackTrace();
			}
			Object[] arr = new Object[3];
			arr[0] = coverImage;
			arr[1] = view;
			arr[2] = pos;
			return arr;

		}

		@Override
		protected void onPostExecute(Object[] arr) {
			Bitmap image = (Bitmap) arr[0];
			View view = (View) arr[1];
			Integer pos = (Integer) arr[2];
			setImage(image, view, pos);

		}

	}
}
